package gappengko;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** JSONP interface for searching a map based on a set of key-value criteria.
 * Returns a JSON array of maps
 * 
 */
public class SearchServlet extends HttpServlet {

  private static final long serialVersionUID = 1L;

  private HttpRequestHandler _handler = new HttpRequestHandler();

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		try {
			CrossDomainUtils.enableCrossOriginResourceSharing(req, res);			
      String json = _handler.search(req).toString();      
			ContentNegotiationUtils.processRequest(req, res, json);
			
		} catch (Exception e) {
		  throw new RuntimeException(e);
		}
	}
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
	
	@Override
	public void doOptions(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
	  CrossDomainUtils.doOptions(req, res);
	}

}
